package com.company.genealogy.web.service.impl;

import com.company.genealogy.web.dto.MemberAuditQueryDTO;
import com.company.genealogy.web.dto.AuditActionDTO;
import com.company.genealogy.web.vo.MemberAuditVO;
import com.company.genealogy.web.service.MemberAuditService;
import com.company.genealogy.web.service.MemberService;
import com.company.genealogy.web.entity.MemberAdditionAudit;
import com.company.genealogy.web.entity.Member;
import com.company.genealogy.web.mapper.MemberAdditionAuditMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
public class MemberAuditServiceImpl implements MemberAuditService {
    @Autowired
    private MemberAdditionAuditMapper auditMapper;
    @Autowired
    private MemberService memberService;

    @Override
    public Page<MemberAuditVO> pageList(MemberAuditQueryDTO query) {
        int offset = (query.getPageNum() - 1) * query.getPageSize();
        List<MemberAuditVO> records = auditMapper.pageList(query.getStatus(), query.getClanId(), offset, query.getPageSize());
        int total = auditMapper.countByStatus(query.getStatus(), query.getClanId());
        Page<MemberAuditVO> page = new Page<>(query.getPageNum(), query.getPageSize(), total);
        page.setRecords(records);
        return page;
    }

    @Override
    public Map<String, Integer> stats(MemberAuditQueryDTO query) {
        Map<String, Integer> result = new HashMap<>();
        List<Map<String, Object>> statsList = (List<Map<String, Object>>) auditMapper.stats(query.getClanId());
        if (statsList == null) {
            statsList = Collections.emptyList();
        }
        for (Map<String, Object> map : statsList) {
            result.put((String) map.get("status"), ((Number) map.get("cnt")).intValue());
        }
        return result;
    }

    @Override
    @Transactional
    public void approve(AuditActionDTO dto) {
        MemberAdditionAudit audit = auditMapper.selectById(dto.getId());
        if (audit == null || !"PENDING".equals(audit.getStatus())) {
            throw new RuntimeException("审核记录不存在或状态不正确");
        }
        // 构造Member对象
        Member member = new Member();
        member.setClanId(audit.getClanId());
        member.setName(audit.getName());
        member.setGender(audit.getGender());
        member.setBirthDate(audit.getBirthDate());
        member.setDeathDate(audit.getDeathDate());
        member.setPhoto(audit.getPhoto());
        member.setStory(audit.getStory());
        member.setNativePlace(audit.getNativePlace());
        member.setCurrentAddress(audit.getCurrentAddress());
        member.setApprovalStatus("APPROVED");
        member.setSubmitterId(audit.getProposerUserId());
        member.setCreatedAt(new Date());
        member.setUpdatedAt(new Date());
        // 关系处理（如父母ID、配偶等可根据需求补充）
        memberService.save(member);
        // 更新审核表状态
        audit.setStatus("APPROVED");
        audit.setAuditorId(dto.getAuditorId());
        audit.setAuditRemarks(dto.getRemarks());
        audit.setUpdatedAt(new Date());
        auditMapper.updateById(audit);
    }

    @Override
    @Transactional
    public void reject(AuditActionDTO dto) {
        MemberAdditionAudit audit = auditMapper.selectById(dto.getId());
        if (audit == null || !"PENDING".equals(audit.getStatus())) {
            throw new RuntimeException("审核记录不存在或状态不正确");
        }
        audit.setStatus("REJECTED");
        audit.setAuditorId(dto.getAuditorId());
        audit.setAuditRemarks(dto.getRemarks());
        audit.setUpdatedAt(new Date());
        auditMapper.updateById(audit);
    }
}